package gamefw.tests;

import junit.framework.TestCase;
import gamefw.framework.GameManager;
import gamefw.gameplugin.GameEntity;
import gamefw.tests.implementations.game.SimpleGameEntity;
import gamefw.tests.implementations.game.SimpleGameFactory;
import gamefw.tests.implementations.ui.SimpleUIFactory;

public class TestFramework extends TestCase {
	
	/**
	 * FW01
	 * Test to check if entities are created correctly
	 */
	public void testCreateEntity() {
		GameManager gm = null;
		SimpleGameFactory gameFactory = new SimpleGameFactory();
		try {
			gm = new GameManager(gameFactory,
										new SimpleUIFactory(), "test");
		} catch (Exception e) {
		}
		assertNotNull(gm);
		assertEquals(gm.getResourceType(), "test");

		GameEntity ge = gm.createEntity("test");
		
		assertEquals(ge.getVisualResource("test"), SimpleGameEntity.TESTRESOURCE);
		
		gameFactory.setCreateNullEntity();
		ge = gm.createEntity("test");
		assertNull(ge);
	}
	
	/*
	 * FW02
	 * Test to check if game is starting correctly.
	 * The game does not enter the game loop, that is, starts with game over.
	 */
	public void testInitGameNoLoops() {
		SimpleGameFactory gameFactory = new SimpleGameFactory();
		this.runTest(gameFactory);
	}

	/*
	 * FW03
	 * Test to check if game is started correctly.
	 * The does enters the game loop and does 5 loops before game over.
	 */
	public void testInitGameWithLoops() {
		SimpleGameFactory gameFactory = new SimpleGameFactory();
		// it runs 5 times and returns gameOver;
		gameFactory.setMaxLoops(5);
		
		this.runTest(gameFactory);
	}
	
	/**
	 * FW04
	 * Test if creation of a game returns Exception when using a null factory
	 */
	public void testCrashInitGame() {
		boolean crashed = false;
		try {
			new GameManager(null, null, "");
		} catch (Exception e) {
			crashed = true;
		}
		assertTrue(crashed);
	}

	/**
	 * FW05
	 * Test if initialization of a game returns Exception when using a null player
	 */	
	public void testCrashNullPlayer() {
		SimpleGameFactory gameFactory = new SimpleGameFactory();

		// now the factory will create null players
		gameFactory.setCreateNullPlayer();

		GameManager gm = null;
		boolean crashed = false;
		try {
			gm = new GameManager(gameFactory, new SimpleUIFactory(), "");
			gm.initGame();
		} catch (Exception e) {
			crashed = true;
		}
		assertTrue(crashed);
	}

	/**
	 * FW06
	 * Test if initialization of a game returns Exception when plugins do not match
	 * regarding resource compatibility.
	 */		
	public void testResourcesDontMatch() {
		SimpleGameFactory gameFactory = new SimpleGameFactory();

		gameFactory.setResourcePropToReturn(false);

		boolean crashed = false;
		try {
			new GameManager(gameFactory, new SimpleUIFactory(), "");
		} catch (Exception e) {
			crashed = true;
		}
		assertTrue(crashed);
	}


	/**
	 * auxiliary method for some tests
	 * 
	 * @param factory a game factory
	 */
	private void runTest(SimpleGameFactory factory) {
		GameManager gm = null;
		try {
			gm = new GameManager(factory,
										new SimpleUIFactory(), "");
		} catch (Exception e) {
		}
		assertNotNull(gm);
		
		try {
			gm.initGame();
		} catch (Exception e) {
			// test not ok
			assertTrue(false);
		}
	}

}
